/*
 * Copyright (c) 2015. Visionet and/or its affiliates. All right reserved.
 * VISIONET PROPRIETARY/CONFIDENTIAL.
 */
package com.visionet.jumper.project.sample.controller;

import javax.annotation.Resource;
import javax.validation.Valid;

import com.visionet.jumper.core.common.Message;
import com.visionet.security.domain.dto.UserDTO;
import com.visionet.security.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.visionet.jumper.core.domain.bean.Page;

import com.visionet.jumper.project.sample.domain.entity.Sample;
import com.visionet.jumper.project.sample.service.SampleService;
import com.visionet.jumper.project.sample.vo.SampleVo;

/**
 * @author Sin
 * @since 11/29/2015.
 */
@Controller
public class SampleController {
	
	@Resource
	private SampleService sampleService;
	@Resource
	private UserService userService;
	
	@RequestMapping("/index")
	public String sampleIndex(Model model) {
		Subject subject = SecurityUtils.getSubject();
		UserDTO userDTO = (UserDTO) subject.getPrincipal();
		model.addAttribute("user", userDTO);
		return "index";
	}

	@RequestMapping("/welcome")
	public String login() {
		return "system/resource";
	}
	
	@RequestMapping("/saveT")
	@ResponseBody
	public Message save(@Valid SampleVo sampleVo, BindingResult result) {
		Message msg = new Message();
		try{
			sampleService.saveSample(sampleVo);
		}catch(Exception e){
			msg.setCode("error");
			msg.setMsg("保存失败！");
		}
		msg.setCode("success");
       return msg;
    }
	
	@RequestMapping("/saveSample")
	@ResponseBody
	public String saveSample() {
		Sample sample = new Sample();
		sample.setName("张三");
		sample.setDescription("AbstracServiceTest");
		sample.setAge(90);
		sampleService.save(sample);
		return "index";
	}
	
	@RequestMapping("/getPage.form")
	public String getPage() {
		Page page = new Page();
		page.setCurPage(1);
		page.setPageSize(5);
		sampleService.getPageByHql(page);
		sampleService.getPageBySql(page);
		return "index";
	}
}